use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(OrderItems::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(OrderItems::Id)
                            .string()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(OrderItems::OrderId).string().not_null())
                    .col(ColumnDef::new(OrderItems::ProductId).string().not_null())
                    .col(ColumnDef::new(OrderItems::OrderNumber).string().not_null())
                    .col(ColumnDef::new(OrderItems::ProductSku).string().not_null())
                    .col(ColumnDef::new(OrderItems::VariantLabel).string().not_null())
                    .col(ColumnDef::new(OrderItems::Name).string().not_null())
                    .col(ColumnDef::new(OrderItems::Image).string().not_null())
                    .col(ColumnDef::new(OrderItems::Quantity).integer().not_null())
                    .col(
                        ColumnDef::new(OrderItems::Price)
                            .decimal_len(16, 4)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(OrderItems::CreatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(OrderItems::UpdatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(OrderItems::DeletedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_order_items_order_id")
                            .from(OrderItems::Table, OrderItems::OrderId)
                            .to(Orders::Table, Orders::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(OrderItems::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum OrderItems {
    Table,
    Id,
    OrderId,
    ProductId,
    OrderNumber,
    ProductSku,
    VariantLabel,
    Name,
    Image,
    Quantity,
    Price,
    CreatedAt,
    UpdatedAt,
    DeletedAt,
}

#[derive(DeriveIden)]
enum Orders {
    Table,
    Id,
}
